package com.example.zx.theline.check;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.os.Bundle;
import android.util.Log;
import android.widget.CompoundButton;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.example.zx.theline.R;

import java.util.ArrayList;
import java.util.List;

import base.BaseFragmentActivity;
import fragment.CheckFragment;
import fragment.CustodyFragment;

public class CheckActivity extends BaseFragmentActivity {

    RadioGroup mRadioGroup;
    ViewPager mViewPager;
    RadioButton mRadioButton1;
    RadioButton mRadioButton2;

    ViewPagerAdapter mViewPagerAdapter;
    List<Fragment> mFragmentList;
    CheckFragment mCheckFragment;
    CustodyFragment mCustodyFragment;




    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_check);
        InitView();
        InitData();
        InitListen();


    }


    protected void InitView() {
        mRadioGroup = (RadioGroup) findViewById(R.id.radiogroup);
        mViewPager = (ViewPager) findViewById(R.id.viewpager);
        mRadioButton1 = (RadioButton) findViewById(R.id.jiandujiancha);
        mRadioButton2 = (RadioButton) findViewById(R.id.shigongjianhu);

    }


    protected void InitData() {
        mViewPagerAdapter = new ViewPagerAdapter(getSupportFragmentManager());
        mFragmentList = new ArrayList<>();
        mCheckFragment = new CheckFragment();
        mCustodyFragment = new CustodyFragment();

        mFragmentList.add(mCheckFragment);
        mFragmentList.add(mCustodyFragment);
        mViewPager.setAdapter(mViewPagerAdapter);

        mRadioButton1.performClick();


    }
    protected void InitListen() {
        //页面切换监听
        mViewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
//                //positon为当前页面，及你点击滑动的页面，positionOffset为切换进度 页面偏移的百分比 1为完成0-1为进行中
//                //positionOffsetPixels:当前页面偏移的像素位置
//                Log.v("position",""+position+"");
//                Log.v("positionOffset",""+positionOffset+"");
//                Log.v("positionOffsetPixels",""+positionOffsetPixels+"");
            }

            @Override
            public void onPageSelected(int position) {
                //页面切换完成以后触发
                if(position==0){
                    mRadioButton1.performClick();
                }
                if(position==1){
                    mRadioButton2.performClick();
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        mRadioButton1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if(isChecked){
                   mRadioButton1.setTextColor(getResources().getColor(R.color.colorRadio));
                   mViewPager.setCurrentItem(0);
                }
                else {
                    mRadioButton1.setTextColor(getResources().getColor(R.color.black));
                }
            }
        });
        mRadioButton2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if(isChecked){
                    mRadioButton2.setTextColor(getResources().getColor(R.color.colorRadio));
                    mViewPager.setCurrentItem(1);
                }
                else {
                    mRadioButton2.setTextColor(getResources().getColor(R.color.black));
                }
            }
        });
    }




    public class ViewPagerAdapter extends FragmentPagerAdapter {


        public ViewPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            return mFragmentList.get(position);
        }

        @Override
        public int getCount() {
            return mFragmentList.size();
        }
    }
}
